package com.jfs.qy.config.component;

import com.jfs.qy.chain.component.LoginComponent;
import com.jfs.qy.enums.TaskKeyEnum;
import com.jfs.qy.service.LogoutService;
import com.jfs.qy.service.SystemInfoService;

import java.net.URISyntaxException;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;

/**
 * @Description 定时查询系统详情任务
 * @E-mail cwm3412638@163.com
 * @Author cwm
 * @Date 2022/11/4
 */
public class SystemInfoTask implements Task {

    private final SystemInfoService systemInfoService;
    private final LogoutService logoutService;
    private final LoginComponent loginComponent;

    public SystemInfoTask(SystemInfoService systemInfoService,LogoutService logoutService,LoginComponent loginComponent) {
        this.systemInfoService = systemInfoService;
        this.logoutService=logoutService;
        this.loginComponent=loginComponent;
    }

    @Override
    public String getKey() {
        return TaskKeyEnum.SYSTEM_INFO_KEY.getKey();
    }

    @Override
    public void run() {
        try {
            systemInfoService.searchSystemInfo();
        }catch (Exception e){
            //注销
            logoutService.logout();
            //再次登录
            try {
                loginComponent.login();
            } catch (URISyntaxException ex) {
                throw new RuntimeException(ex);
            } catch (NoSuchAlgorithmException ex) {
                throw new RuntimeException(ex);
            } catch (KeyStoreException ex) {
                throw new RuntimeException(ex);
            } catch (KeyManagementException ex) {
                throw new RuntimeException(ex);
            }
            e.printStackTrace();
        }
    }
}
